﻿
namespace Patterns4Net.ArchitectureExplorer.Infrastructure
{
    using System;
    using System.IO;
    using Caliburn.Micro;

    public class FileLogger : ILog
    {
        private readonly string filename;

        public FileLogger(string filename)
        {
            this.filename = filename;
        }

        public void Info(string format, params object[] args)
        {
            using (var writer = new StreamWriter(this.filename, true))
            {
                StartNewEntry(writer);
                writer.WriteLine(format, args);
            }
        }

        public void Warn(string format, params object[] args)
        {
            using (var writer = new StreamWriter(this.filename, true))
            {
                StartNewEntry(writer);
                writer.WriteLine(format, args);
            }
        }

        public void Error(Exception exception)
        {
            using (var writer = new StreamWriter(this.filename, true))
            {
                StartNewEntry(writer);
                writer.WriteLine("Exception: {0}.", exception.GetType());
                writer.WriteLine("Message: {0}.", exception.Message);
                writer.WriteLine("StackTrace: {0}.", exception.StackTrace);
            }
        }

        private static void StartNewEntry(StreamWriter writer)
        {
            writer.WriteLine();
            writer.WriteLine("------------");
            writer.WriteLine(DateTime.Now);
        }
    }
}
